﻿using System;
using System.Collections.Generic;//c#泛型集合 
using System.IO;

namespace RansackFile
{
    //获取文件夹下的所有子文件夹及文件的名称
    class Program
    {
        static void Main(string[] args)
        {
            
            Dictionary<string,long> firstDict = new Dictionary<string, long>();
            Dictionary<string,long> files =GetFile("G:",firstDict);
            foreach(var f in files)
            {
                System.Console.WriteLine(f.Key);
              
            }
        }

        //Dictionary的主要用途是提供快速的基于兼职的元素查找
        ///<summary>
        ///获取路径下所有文件及子文件夹中文件
        ///</summary>
        ///<param name = "path">全路径根目录</param>
        ///<param name = "fileList">存放所有文件的全路径</param>
        ///<returns></returns>
        public static Dictionary<string,long> GetFile(string path,Dictionary<string,long> fileList)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] fil = dir.GetFiles();
            DirectoryInfo[] dii = dir.GetDirectories();
            foreach(FileInfo f in fil)
            {
                long size = f.Length;
                fileList.Add(f.FullName,size);//添加文件路径到列表中

            }
            //获取子文件夹内的文件列表，递归遍历
            foreach(DirectoryInfo d in dii)
            {
                    GetFile(d.FullName,fileList);
            }
            return fileList;
        }
    }
}
